<template>
  <div class="home">
    <el-container class="home-container">
      <el-header>
        <div class="logobox">
          <img
            src="../../assets/heima.png"
            alt=""
          >
          <span>电商后台管理</span>
        </div>
        <el-button
          type='info'
          @click="logout"
        >
          退出
        </el-button>
      </el-header>
      <!-- 侧边栏区域 -->
      <el-container>
        <el-aside :width="flod?'64px':'200px'">
          <el-menu
            :default-active="keepPath"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="skyblue"
            :unique-opened='true'
            :collapse='flod'
            :collapse-transition='false'
            :router='true'
          >
            <div
              class="fold"
              @click="flodAddUnflod"
            ><i class="el-icon-s-fold"></i></div>
            <!-- 一级导航栏 -->
            <!-- index控制左侧栏的打开 -->
            <el-submenu
              :index="item.id+''"
              v-for="item in menusList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="menuIconsObj[item.id]"></i>
                <span class="authname">{{item.authName}}</span>
              </template>
              <!-- 二级导航栏 -->
              <el-menu-item
                v-for="sonitem in item.children"
                :index="'/'+sonitem.path"
                :key="sonitem.id"
                @click="keepCheck('/'+sonitem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{sonitem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      menusList: [],
      menuIconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      flod: false,
      // 被激活的链接地址
      keepPath: ''
    }
  },
  created() {
    this.getmenus()
    this.keepPath = window.sessionStorage.getItem('keepPath')
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getmenus() {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error('请求失败')
      }
      this.menusList = res.data
    },
    // 左侧栏的折叠与展开
    flodAddUnflod() {
      this.flod = !this.flod
    },
    // 保存链接的激活状态
    keepCheck(keepPath) {
      window.sessionStorage.setItem('keepPath', keepPath)
      // 切换重新赋值高亮
      this.keepPath = keepPath
    }
  }
}
</script>
<style lang="scss">
.home {
  height: 100%;
  .home-container {
    height: 100%;
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0px;
    .logobox {
      display: flex;
      align-items: center;
      span {
        margin-left: 20px;
        color: #fff;
      }
    }
  }
  .el-aside {
    background-color: #373d41;
    .authname {
      margin-left: 10px;
    }
    .el-menu {
      border-right: none;
      .fold {
        background-color: #4a5064;
        text-align: center;
        height: 30px;
        line-height: 30px;
      }
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>